package top.haijunit.mark.common.mybatis.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author 百旺张
 * @Date 2021/11/9
 * @description
 */
public class MyBatisPlusGenerator {

    private static final String url = "jdbc:mysql://127.0.0.1:3306/bucket_security?characterEncoding=utf8&createDatabaseIfNotExist=true&serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull";


    public static void main(String[] args) {
        FastAutoGenerator.create(url, "root", "123456")
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("百旺张").enableSwagger().fileOverride().outputDir("D://Generator");
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.parent("com.bucket.generator")
                            .moduleName("mybatis")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://Generator"));
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder.addExclude("databasechangelog","databasechangeloglock")
                            .addInclude(getTables("ALL")).addTablePrefix("tb_", "t_");
                })
                // 模板引擎配置，默认 Velocity
                .templateEngine(new FreemarkerTemplateEngine()).execute();
    }

    /** 处理 all 情况 */
    protected static List<String> getTables(String tables) {
        return "all".equalsIgnoreCase(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
